<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\ActivityModel;
use Illuminate\Http\Request;
use App\Services\ActivityService;
use Session;
use App\Services\CommonService;

class ActivityController extends Controller{
    public function SignUserList($activityid){
        $username = Session::get('username');
        if(!$username){
            return redirect('/admin/login');
        }else{
            $activity = new ActivityService;
            $signList = $activity->getSignActivityList($activityid);
            $activityInfo = $activity->getActivityInfoById($activityid);
            $data['username'] = $username;
            $data['activityInfo'] = $activityInfo;
            $data['signList'] = $signList;

            return view('admin.activity.sign_list',$data);
        }
    }

	public function list(Request $request){
		$username = Session::get('username');
		if(!$username){
			return redirect('/admin/login');
		}else{
			date_default_timezone_set('PRC');

			$data['username'] = $username;
			$data['page_size_list'] = array(20,25,50,100);
			$data['page_size'] = $request->input('page_size') ? intval($request->input('page_size')) : 15;
			$data['keywords'] = $request->input('keywords') ? addslashes($request->input('keywords')) : '';

			$activity = new ActivityService;
			$data['activitylist'] = $activity->getActivityList($data['page_size'],$data['keywords']);

			return view('admin.activity.list',$data);
		}
	}

	public function create(){
		$username = Session::get('username');
		if(!$username){
			return redirect('/admin/login');
		}else{
			$data['username'] = $username;
			$common = new CommonService;
			$data['provincelist'] = $common->getProvinceList();
			return view('admin.activity.add',$data);
		}
	}	

	public function store(){

		date_default_timezone_set('PRC');
		$activity = new ActivityService;

		$_t = time();

		$data['name'] = addslashes($_POST['name']);
		$data['quota'] = addslashes($_POST['quota']);
		$data['desc'] = addslashes($_POST['desc']);
		$data['thumb'] = addslashes($_POST['thumb']);
		$data['thumb2'] = addslashes($_POST['thumb2']);
		$data['content'] = addslashes($_POST['content']);
		$data['start_date'] = addslashes($_POST['start_date']);
		$data['end_date'] = addslashes($_POST['end_date']);
		$data['activity_date'] = addslashes($_POST['activity_date']);
		$data['province'] = intval($_POST['province']);
		$data['city'] = intval($_POST['city']);
		$data['county'] = intval($_POST['county']);
		$data['address'] = addslashes($_POST['address']);
		$data['price'] = addslashes($_POST['price']);

		if($_t >= strtotime($data['start_date']) && $_t <= strtotime($data['end_date'])){
		    $data['status'] = 1;
        }elseif($_t >= strtotime($data['start_date']) && $_t > strtotime($data['end_date'])){
		    $data['status'] = 0;
        }elseif($_t < strtotime($data['start_date']) && $_t < strtotime($data['end_date'])){
		    $data['status'] = 2;
        }

		$data['add_date'] = date('Y-m-d H:i:s');
		$data['isshow'] = 1;

		$info = $activity->getActivityInfoByName($data['name']);

		if($info){
			$_data = array('code'=>300);
		}else{
			$res = $activity->insertActivity($data);
			if($res){
				$_data = array('code'=>200);
			}else{
				$_data = array('code'=>400);
			}
		}

		return response()->json($_data);
	}

	public function edit($id){
		$username = Session::get('username');
		if(!$username){
			return redirect('/admin/login');
		}else{
			$data['username'] = $username;
			$common = new CommonService;
			$data['provincelist'] = $common->getProvinceList();

			$activity = new ActivityService;
			$data['info'] = $activity->getActivityInfoById($id);

			return view('admin.activity.edit',$data);
		}
	}

	public function update(){
		
		date_default_timezone_set('PRC');
		$id = intval($_POST['id']);
		$_t = time();

		$data['name'] = addslashes($_POST['name']);
        $data['quota'] = addslashes($_POST['quota']);
		$data['desc'] = addslashes($_POST['desc']);
		$data['thumb'] = addslashes($_POST['thumb']);
		$data['thumb2'] = addslashes($_POST['thumb2']);
		$data['content'] = addslashes($_POST['content']);
		$data['start_date'] = addslashes($_POST['start_date']);
		$data['end_date'] = addslashes($_POST['end_date']);
		$data['activity_date'] = addslashes($_POST['activity_date']);
		$data['province'] = intval($_POST['province']);
		$data['city'] = intval($_POST['city']);
		$data['county'] = intval($_POST['county']);
		$data['address'] = addslashes($_POST['address']);
		$data['price'] = addslashes($_POST['price']);

        if($_t >= strtotime($data['start_date']) && $_t <= strtotime($data['end_date'])){
            $data['status'] = 1;
        }elseif($_t >= strtotime($data['start_date']) && $_t > strtotime($data['end_date'])){
            $data['status'] = 0;
        }elseif($_t < strtotime($data['start_date']) && $_t < strtotime($data['end_date'])){
            $data['status'] = 2;
        }


		$activity = new ActivityService;

		$res = $activity->modiActivity($id,$data);
		if($res){
			$_data = array('code'=>200);
		}else{
			$_data = array('code'=>400);
		}

		return response()->json($_data);

	}

	public function del($id){
		date_default_timezone_set('PRC');
		$activity = new ActivityService;

		$data['isshow'] = 0;
		$data['modi_date'] = date('Y-m-d H:i:s');

		$res = $activity->modiActivity($id,$data);
		if($res){
			$_data = array('code'=>200);
		}else{
			$_data = array('code'=>400);
		}

		return response()->json($_data);
	}
}